package com.cnblsp2.android.study.dbandio.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cnblsp2.android.study.dbandio.R;

public class DictActivity extends Activity {

	private MyDatabaseHelper dbHelper;
	Button insertBtn = null;
	Button searchBtn = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_dict);
		// 创建MyDataBaseHelper对象，指定数据库版本为1，此处使用相对路径即可
		// 数据库文件会自动保存在程序的数据库文件夹的databases目录下
		dbHelper = new MyDatabaseHelper(this, "myDict.db3", 1);

		insertBtn = (Button) this.findViewById(R.id.activity_dict_insert_btn);
		searchBtn = (Button) this.findViewById(R.id.activity_dict_search_btn);

		insertBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String word = ((EditText) findViewById(R.id.activity_dict_word_et)).getText().toString();
				String detail = ((EditText) findViewById(R.id.activity_dict_detail_et)).getText().toString();

				// 插入生词记录
				insertData(dbHelper.getReadableDatabase(), word, detail);

				Toast.makeText(DictActivity.this, "添加重启成功!", 8000).show();

			}
		});

		searchBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String key = ((EditText) findViewById(R.id.activity_dict_searchkey_et)).getText().toString();
				Cursor cursor = dbHelper.getReadableDatabase().rawQuery("select * from dict where word like ? or detail like ? ",
						new String[] { "%" + key + "%", "%" + key + "%" });
				// 创建一个Bundle对象
				Bundle data = new Bundle();
				data.putSerializable("data", converCursorToList(cursor));
				Intent intent = new Intent(DictActivity.this, WordSearchResultActivity.class);
				intent.putExtras(data);
				startActivity(intent);
			}
		});

	}

	protected ArrayList<Map<String, String>> converCursorToList(Cursor cursor) {

		ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();

		while (cursor.moveToNext()) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("word", cursor.getString(1));
			map.put("detail", cursor.getString(2));

			result.add(map);
		}
		return result;

	}

	private void insertData(SQLiteDatabase db, String word, String detail) {
		db.execSQL("insert into dict values(null,?,?) ", new String[] { word, detail });
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}

}
/**




















*/
